﻿using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;

namespace LogikBug.ModularMvc.Navigation
{
	public class MenuItem
	{
		#region Fields

		private string _text;

		#endregion

		#region Constructors

		public MenuItem(string href)
		{
			Href = href;
			MenuItems = new MenuItemCollection();
			Attributes = new Dictionary<string, string>();
		}

		public MenuItem(string href, string text)
			: this(href)
		{
			_text = text;
		}

		public MenuItem(string href, string text, string id, string cssClass)
			: this(href, text)
		{
			Id = id;
			CssClass = cssClass;
		}

		public MenuItem(string href, string text, string id, string cssClass, string name, string title, string target)
			: this(href, text, id, cssClass)
		{
			Name = name;
			Title = title;
			Target = target;
		}

		#endregion

		#region Properties

		public string CssClass { get; set; }
		public string Id { get; set; }
		public string Href { get; set; }
		public string Name { get; set; }
		public string Target { get; set; }
		public string Text
		{
			get
			{
				return string.IsNullOrEmpty(_text) ? Href : _text;
			}
			set
			{
				_text = value;
			}
		}
		public string Title { get; set; }
		public string Data { get; set; }
		public IDictionary<string, string> Attributes { get; private set; }
		public MenuItemCollection MenuItems { get; private set; }

		#endregion

		#region Methods

		public override string ToString()
		{
			return ToMvcHtmlString().ToHtmlString();
		}

		public virtual MvcHtmlString ToMvcHtmlString()
		{
			return ToMvcHtmlString(MenuNode.Li);
		}

		public MvcHtmlString ToMvcHtmlString(MenuNode menuNode)
		{
			HtmlAttributes();

			var sb = new StringBuilder(menuNode.OpenTag);
			var tb = new TagBuilder(MenuNode.A.TagName);

			tb.MergeAttributes(Attributes);
			tb.InnerHtml = Text;
			sb.Append(tb.ToString());

			if (MenuItems.Count > 0)
			{
				sb.Append(menuNode.ParentOpenTag);

				foreach (var menuItem in MenuItems)
				{
					sb.Append(menuItem.ToMvcHtmlString(menuNode));
				}

				sb.Append(menuNode.ParentCloseTag);
			}

			return MvcHtmlString.Create(sb.Append(menuNode.CloseTag).ToString());
		}

		public void HtmlAttributes()
		{
			AddAttributeIfNotNullOrEmpty(Attributes, "id", Id);
			AddAttributeIfNotNullOrEmpty(Attributes, "name", Name);
			AddAttributeIfNotNullOrEmpty(Attributes, "href", Href);
			AddAttributeIfNotNullOrEmpty(Attributes, "title", Title);
			AddAttributeIfNotNullOrEmpty(Attributes, "target", Target);
			AddAttributeIfNotNullOrEmpty(Attributes, "class", CssClass);
			AddAttributeIfNotNullOrEmpty(Attributes, "data", Data);
		}

		public void AddAttribute(string name, string value)
		{
			AddAttributeIfNotNullOrEmpty(Attributes, name, value);
		}

		private static void AddAttributeIfNotNullOrEmpty(IDictionary<string, string> attributes, string name, string value)
		{
			if (!string.IsNullOrEmpty(value))
			{
				attributes[name] = value;
			}
		}

		#endregion
	}
}
